package ontocompo.ui.merge;

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.util.Observable;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;

import ontocompo.ui.selection.Selector;

public class PlugSelector extends Selector {

	private JComponent firstComponent;
	private JComponent secondComponent;
	
	private Border borderBak1;
	private Color bgBak1;
	private boolean opaqueBak1;
	
	private Border borderBak2;
	private Color bgBak2;
	private boolean opaqueBak2;
	
	public PlugSelector() {
		super();
	}
	
	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		JComponent over = (JComponent) arg0.getSource();
		
//		System.err.println("firstComponent: " + firstComponent);
//		System.err.println("secondComponent: " + secondComponent);
		
		if(firstComponent == null) {
			this.firstComponent = over;
			this.borderBak1 = over.getBorder();
			this.bgBak1 = over.getBackground();
			this.opaqueBak1 = over.isOpaque();
			
			over.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED, Color.red, Color.yellow));
			over.setBackground(this.selectedColor);
			over.setOpaque(true);
			
			this.setChanged();
			this.notifyObservers("firstComponentSelected");
		} else {
		
			if(secondComponent == null) {
				if(firstComponent.equals(over)) {
					firstComponent.setBorder(this.borderBak1);
					firstComponent.setBackground(this.bgBak1);
					firstComponent.setOpaque(this.opaqueBak1);
				
					this.firstComponent = null;
					
					this.setChanged();
					this.notifyObservers("cancelFirstComponent");
				} else {
					this.secondComponent = over;
					this.borderBak2 = over.getBorder();
					this.bgBak2 = over.getBackground();
					this.opaqueBak2 = over.isOpaque();
					
					over.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED, Color.red, Color.yellow));
					over.setBackground(this.selectedColor);
					over.setOpaque(true);
					
					this.setChanged();
					this.notifyObservers("secondComponentSelected");
				}
			} else {
				if(secondComponent.equals(over)) {
					secondComponent.setBorder(this.borderBak2);
					secondComponent.setBackground(this.bgBak2);
					secondComponent.setOpaque(this.opaqueBak2);
				
					this.secondComponent = null;
					
					this.setChanged();
					this.notifyObservers("cancelSecondComponent");
				}
			}
			
		}
	}
	
	public void resetSelection() {
		if(this.firstComponent != null) {
			firstComponent.setBorder(this.borderBak1);
			firstComponent.setBackground(this.bgBak1);
			firstComponent.setOpaque(this.opaqueBak1);
		
			this.firstComponent = null;
		}
		
		if(this.secondComponent != null) {
			secondComponent.setBorder(this.borderBak2);
			secondComponent.setBackground(this.bgBak2);
			secondComponent.setOpaque(this.opaqueBak2);
		
			this.secondComponent = null;
		}
		
		this.setChanged();
		this.notifyObservers("cancelFirstComponent");
	}
	
	@Override
	public void mouseEntered(MouseEvent arg0) {
		super.mouseEntered(arg0);
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		JComponent over = (JComponent) arg0.getSource();
		
		if(firstComponent == null || (firstComponent != null && !over.equals(firstComponent) && secondComponent == null) || (firstComponent != null && !over.equals(firstComponent) && secondComponent == null && !over.equals(secondComponent))) {
			super.mouseExited(arg0);
		}
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		//super.mousePressed(arg0);
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		//super.mouseReleased(arg0);
	}
	
	public JComponent getFirstSelectedComponent() {
		return this.firstComponent;
	}
	
	public JComponent getSecondSelectedComponent() {
		return this.secondComponent;
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		
	}
}